<?php
class ImageFunction {

    private $Oimage;	// Link Ảnh gốc
    private $image;		// Link Ảnh mới
    private $image_type;// Loại ảnh
    public $image_height;// Độ cao gốc của ảnh
    public $image_width;// Độ rộng gốc của ảnh
    public $first = 0; //Kiểm tra xem ảnh đã dc resize thành kiểu 1024x768 hay chưa;

    /**
     * Load ảnh:
     */

    function load($filename) {
        $image_info = getimagesize($filename);
        $this->image_type = $image_info[2];
        $this->image_width = (int) $image_info[0];
        $this->image_height = (int) $image_info[1];

        if ($this->image_type == IMAGETYPE_JPEG)
            $this->image = imagecreatefromjpeg($filename);
        elseif ($this->image_type == IMAGETYPE_GIF)
            $this->image = imagecreatefromgif($filename);
        elseif ($this->image_type == IMAGETYPE_PNG)
            $this->image = imagecreatefrompng($filename);
        $this->Oimage = $this->image;
    }

    /**
     * Lưu ảnh:
     */
    function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 100, $permissions = null) {
        if ($image_type == IMAGETYPE_JPEG)
            $return = imagejpeg($this->image, $filename, $compression);
        elseif ($image_type == IMAGETYPE_GIF)
            $return = imagegif($this->image, $filename,9);
        elseif ($image_type == IMAGETYPE_PNG)
            $return = imagepng($this->image, $filename,9);
        if ($permissions != null)
            chmod($filename, $permissions);
        return $return;
    }

//     function output($image_type = IMAGETYPE_JPEG) {
//         if ($image_type == IMAGETYPE_JPEG)
//             imagejpeg($this->image);
//         elseif ($image_type == IMAGETYPE_GIF)
//             imagegif($this->image);
//         elseif ($image_type == IMAGETYPE_PNG)
//             imagepng($this->image);
//     }

    /**
     * Lấy chiều rộng của ảnh:
     */
    function getWidth() {
    	return $this->image_width;
        //return imagesx($this->Oimage);
    }

    /**
     * Lấy chiều cao của ảnh:
     */
    function getHeight() {
    	return $this->image_height;
        //return imagesy($this->Oimage);
    }

    /**
     * Resize theo chiều cao:
     */
    function resizeToHeight($height) {
        if ($height >= $this->getHeight())
            $height = $this->getHeight();
        $ratio = $height / $this->getHeight();
        $width = $this->getWidth() * $ratio;
        return $this->resize($width, $height);
    }

    /**
     * Resize theo chiều rộng:
     */
    function resizeToWidth($width) {
        if ($width > $this->getWidth())
            $width = $this->getWidth();
        $ratio = $width / $this->getWidth();
        $height = $this->getheight() * $ratio;
        return $this->resize($width, $height);
    }

    /**
     * Resize ảnh theo tỉ lệ:
     */
    function scale($scale) {
        $width = $this->getWidth() * $scale / 100;
        $height = $this->getheight() * $scale / 100;
        return $this->resize($width, $height);
    }

    /**
     * Resize ảnh theo chiều cao và chiều rộng:
     */
    function resize($width, $height) {
        $width = (int) $width;
        $height = (int) $height;

        if ($width > $this->getWidth() && $height > $this->getHeight()) {
            $new_w = $this->getWidth();
            $new_h = $this->getHeight();
        } else {
            $ratio = $this->getWidth() / $this->getHeight();
            $target_ratio = $width / $height;
            if ($ratio > $target_ratio) {
                $new_w = $width;
                $new_h = round($width / $ratio);
            } else {
                $new_h = $height;
                $new_w = round($height * $ratio);
            }
        }
        $new_image = imagecreatetruecolor($new_w, $new_h);
//        if ($this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG) {
//            $current_transparent = imagecolortransparent($this->Oimage);
//            if ($current_transparent != -1) {
//                $transparent_color = imagecolorsforindex($this->Oimage, $current_transparent);
//                $current_transparent = imagecolorallocate($new_image, $transparent_color['white'], $transparent_color['green'], $transparent_color['blue']);
//                imagefill($new_image, 0, 0, $current_transparent);
//                imagecolortransparent($new_image, $current_transparent);
//            } elseif ($this->image_type == IMAGETYPE_PNG) {
//                imagealphablending($new_image, false);
//                $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
//                imagefill($new_image, 0, 0, $color);
//                imagesavealpha($new_image, true);
//            }
//        }
        if (!imagecopyresampled($new_image, $this->Oimage, 0, 0, 0, 0, $new_w, $new_h, $this->getWidth(), $this->getHeight()))
            return false;
        $this->image = $new_image;

        return true;
    }

    function forceresize($width, $height) {
        $ratio = $this->getWidth() / $this->getHeight();
        $target_ratio = $width / $height;
        if ($ratio > $target_ratio) {
            $new_w = $width;
            $new_h = round($width / $ratio);
        } else {
            $new_h = $height;
            $new_w = round($height * $ratio);
        }
        $new_image = imagecreatetruecolor($new_w, $new_h);
        imagecopyresampled($new_image, $this->Oimage, 0, 0, 0, 0, $new_w, $new_h, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }

    function _resize($width, $height) {
        $ratio = $this->getWidth() / $this->getHeight();
        $target_ratio = $width / $height;
        if ($ratio > $target_ratio) {
            $new_w = $width;
            $new_h = round($width / $ratio);
        } else {
            $new_h = $height;
            $new_w = round($height * $ratio);
        }
        $new_image = imagecreatetruecolor($new_w, $new_h);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_w, $new_h, $this->getWidth(), $this->getHeight());
        //create background
        $bg_image = imagecreatefromjpeg('bg.jpg');
        $new_image_bg = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image_bg, $bg_image, 0, 0, 0, 0, $width, $height, 1920, 1200);
        $start_w = ($width - $new_w) / 2;
        $start_h = ($height - $new_h) / 2;
        imagecopy($new_image_bg, $new_image, $start_w, $start_h, 0, 0, $new_w, $new_h);
        $this->image = $new_image_bg;
    }

    function allocateImg() {
        $this->Oimage = $this->image;
    }

}